<!DOCTYPE html>
<html lang='en'>
  <head>
    <meta content='text/html; charset=utf-8' http-equiv='Content-Type'>
    <title>Sample Page</title>
    <style></style>
     <link href="/static/css/bootstrap.css" rel="stylesheet">
     <script type="text/javascript" src="/static/js/websocket.js"></script>
    <style type="text/css">
      body {
        padding-top: 60px;
        padding-bottom: 40px;
      }
    </style>

</head>
<body>

    <div class="navbar navbar-inverse navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </a>
          <a class="brand" href="#">Project name</a>
          <div class="nav-collapse collapse">
            <ul class="nav">
              <li class="active"><a href="#">Home</a></li>
              <li><a href="#about">About</a></li>
              <li><a href="#contact">Contact</a></li>
              <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
                <ul class="dropdown-menu">
                  <li><a href="#">Action</a></li>
                  <li><a href="#">Another action</a></li>
                  <li><a href="#">Something else here</a></li>
                  <li class="divider"></li>
                  <li class="nav-header">Nav header</li>
                  <li><a href="#">Separated link</a></li>
                  <li><a href="#">One more separated link</a></li>
                </ul>
              </li>
            </ul>
            <form class="navbar-form pull-right">
              <button type="submit" class="btn">Join</button>
            </form>
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>


    <div class="container">
    <div id="status"></div>
    <ul id="mylist">
    </ul>
    <form action="javascript:createField();">
        Name:<br/><input type="text" id="name"/>
        <!--<input type="submit" value="Create"/> -->
</form>
    </div>
    <script>
    </script>
  </body>
<script type="text/javascript">

function addStatus(text){
	var date = new Date();
//	document.getElementById('status').innerHTML
//		= document.getElementById('status').innerHTML
//		+ date + ": " + text + "<br/>";
    console.log( date + ":" + text);
}

function updateRace(listOfRaces){
	var ul = document.getElementById('mylist');
    ul.innerHTML = "";
    for ( var race in listOfRaces ){
        var newElement = document.createElement('li');
        var newLink = document.createElement('a');
        newLink.setAttribute('href','/race/'+listOfRaces[race]);
        newLink.innerHTML=listOfRaces[race];
        newElement.appendChild(newLink);
        ul.insertBefore(newElement,ul.firstChild);
    }
}

function addField(data){
}

function createField(){
	var name = document.getElementById('name').value;
    ws.send("CREATE"+name);
    document.getElementById('name').value='';
}

function myonmessage(evt) {
    var receivedMsg = evt.data; 
    console.log(evt.data);
                if (evt.data.charAt(0)=='[') updateRace(eval("("+evt.data+")"))    
                addStatus(evt.data);
		};

connect('list',myonmessage, function(evt){ ws.send('LIST');});
</script>
</html>

